package com.vast.ui.component.toolbar;

import java.util.concurrent.Executor;

import org.eclipse.swt.widgets.Composite;

import com.vast.ui.component.controller.TableViewerController;
import com.vast.ui.component.socketinterface.listner.IReceiveListener;
import com.vast.ui.component.socketinterface.listner.ISocketConnectionListener;
import com.vast.ui.component.socketinterface.request.Request;
import com.vast.ui.component.socketinterface.request.RequestQueue;
import com.vast.ui.component.socketinterface.serverblocking.BlockingServer;
import com.vast.ui.component.socketinterface.servercommon.IServer;
import com.vast.ui.component.state.Sequence;
import com.vast.ui.component.util.ConsoleLoggerSingleton;
import com.vast.ui.component.util.Logger;
import com.vast.ui.panel.ledpanel.views.ViewPart;

public class InterfaceLaunchar extends ToolBarAction implements ISocketConnectionListener{
	public  IReceiveListener receiver;
	private IServer server;
	private final RequestQueue sendQueue;
	private final Executor   executor;
	public  final Logger     logger;
	
	public InterfaceLaunchar(ViewPart view, Composite parent, IReceiveListener receiver, int serverPort) {
		super(view);
		this.receiver   = receiver;	
		this.logger     = ConsoleLoggerSingleton.getInstance();
		this.executor   = new Executor() {
			public void execute(Runnable r) {
				new Thread(r).start();
			}
		};
		sendQueue               = new RequestQueue();
		Sequence sequence       = new Sequence();
		server          = new BlockingServer (serverPort, sequence, sendQueue, executor);
		
		//server.addSocketConnectionListener(this);
		if(receiver != null) {
			server.addReceiveListener(receiver);
		}
	}
	
	public void addReceiveListener(IReceiveListener listener){
		server.addReceiveListener(listener);
	}
	
	public void removeReceiveListener(IReceiveListener listener) {
		server.removeReceiveListener(listener);
	}

	@Override
	public void addAction() {
		// TODO Auto-generated method stub
	}

	@Override
	public void deleteAction() {
		// TODO Auto-generated method stub
	}

	@Override
	public void startAction() {
		// TODO Auto-generated method stub
		server.openConnection();
		setStopActionEnable();
	}

	@Override
	public void stopAction() {
		// TODO Auto-generated method stub
		server.closeConnection();
		setStartActionEnable();
	}

	@Override
	public void stateChange(Sequence sequence) {
		// TODO Auto-generated method stub
		if(sequence.isConnecting()) {
			setStopActionEnable();
		} else {
			setStartActionEnable();
		}
	}

}
